package cn.iocoder.yudao.module.alipay.controller.admin.cpsactivitytype;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.alipay.controller.admin.cpsactivitytype.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.cpsactivitytype.CpsActivityTypeDO;
import cn.iocoder.yudao.module.alipay.convert.cpsactivitytype.CpsActivityTypeConvert;
import cn.iocoder.yudao.module.alipay.service.cpsactivitytype.CpsActivityTypeService;

@Tag(name = "管理后台 - cps活动分类")
@RestController
@RequestMapping("/alipay/cps-activity-type")
@Validated
public class CpsActivityTypeController {

    @Resource
    private CpsActivityTypeService cpsActivityTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建cps活动分类")
    @PreAuthorize("@ss.hasPermission('alipay:cps-activity-type:create')")
    public CommonResult<Long> createCpsActivityType(@Valid @RequestBody CpsActivityTypeCreateReqVO createReqVO) {
        return success(cpsActivityTypeService.createCpsActivityType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新cps活动分类")
    @PreAuthorize("@ss.hasPermission('alipay:cps-activity-type:update')")
    public CommonResult<Boolean> updateCpsActivityType(@Valid @RequestBody CpsActivityTypeUpdateReqVO updateReqVO) {
        cpsActivityTypeService.updateCpsActivityType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除cps活动分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('alipay:cps-activity-type:delete')")
    public CommonResult<Boolean> deleteCpsActivityType(@RequestParam("id") Long id) {
        cpsActivityTypeService.deleteCpsActivityType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得cps活动分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('alipay:cps-activity-type:query')")
    public CommonResult<CpsActivityTypeRespVO> getCpsActivityType(@RequestParam("id") Long id) {
        CpsActivityTypeDO cpsActivityType = cpsActivityTypeService.getCpsActivityType(id);
        return success(CpsActivityTypeConvert.INSTANCE.convert(cpsActivityType));
    }

    @GetMapping("/list")
    @Operation(summary = "获得cps活动分类列表")
    @PreAuthorize("@ss.hasPermission('system:dept:query')")
    public CommonResult<List<CpsActivityTypeRespVO>> getDeptList(CpsActivityTypeExportReqVO reqVO) {
        List<CpsActivityTypeDO> list = cpsActivityTypeService.getTypeList(reqVO);
        list.sort(Comparator.comparing(CpsActivityTypeDO::getSort));
        return success(CpsActivityTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得cps活动分类分页")
    @PreAuthorize("@ss.hasPermission('alipay:cps-activity-type:query')")
    public CommonResult<PageResult<CpsActivityTypeRespVO>> getCpsActivityTypePage(@Valid CpsActivityTypePageReqVO pageVO) {
        PageResult<CpsActivityTypeDO> pageResult = cpsActivityTypeService.getCpsActivityTypePage(pageVO);
        return success(CpsActivityTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出cps活动分类 Excel")
    @PreAuthorize("@ss.hasPermission('alipay:cps-activity-type:export')")
    @OperateLog(type = EXPORT)
    public void exportCpsActivityTypeExcel(@Valid CpsActivityTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CpsActivityTypeDO> list = cpsActivityTypeService.getCpsActivityTypeList(exportReqVO);
        // 导出 Excel
        List<CpsActivityTypeExcelVO> datas = CpsActivityTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "cps活动分类.xls", "数据", CpsActivityTypeExcelVO.class, datas);
    }

    @GetMapping("/getTreeTypeList")
    @Operation(summary = "获取分类列表Tree")
    public CommonResult<Object> getTreeTypeList(@Valid CpsActivityTypeExportReqVO reqVO) {
        return success(cpsActivityTypeService.getTreeTypeList(reqVO));
    }

}
